package com.lanou3g.k12.web.controller;

import com.lanou3g.k12.service.ImageHandlerService;
import com.lanou3g.k12.utils.K12Result;
import com.lanou3g.k12.utils.K12ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@RestController
public class ImageController {

    @Autowired
    ImageHandlerService imageHandlerService;

    @PostMapping("/upload/image")
    public K12Result uploadImage(@RequestParam MultipartFile image) throws IOException {
        InputStream inputStream = image.getInputStream();
        if (image != null && !image.isEmpty()) {
            String imageSrc = null;
            try {
                imageSrc = imageHandlerService.getimageSrc(inputStream);
                System.out.println(imageSrc);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return K12ResultUtils.ok(imageSrc);
        }
        else {
            return K12ResultUtils.error(410, "上传文件格式异常");
        }
    }


}
